//
// Created by xuan on 2024/4/22.
//

#include <string.h>
#include "UPRE_BSP_WiFi.h"
#include "stdbool.h"
#include "stdio.h"
#include "usart.h"
#include "cmsis_os.h"

bool flag_wifiFinish = false;
bool flag_connectFinish = false;
bool flag_linkError = false;
bool flag_finsh = true;

/////WFMODE:0   UDP 1   TCP
#define WFMODE 1

//// WFTCP_MODE:0   服务端 1   客户端
#define WFTCP_MODE 0

#if WFMODE == 1

const char *SetRestore = "AT+RESTORE\r\n";
const char *SetWFMode = "AT+CWMODE=3\r\n";
const char *SetConnectAP = "AT+CWJAP=";
const char *SetConnectTCP = "AT+CIPSTART=";
const char *SetCIPCommunicateMode = "AT+CIPMODE=1\r\n";
const char *SetTransparentMode = "AT+CIPSEND\r\n";

const char *SetCommunicateMode = "AT+CIPMUX=1\r\n";
const char *SetServerMode = "AT+CIPSERVER=1\r\n";
const char *Send = "AT+CIPSEND=";

const char *ssid = "ATK_6253FF";
const char *key = "";
const char *WiFi_Mode = "TCP";
const char *remote_host = "192.168.4.1";
const char *remote_port = "333";
const char *locate_port = "333";
const char *UDP_MODE = "0";

uint8_t WiFi_rx[WIFI_MESSAGE_SIZE] = {0};
uint8_t buffer[WIFI_MESSAGE_SIZE] = {0};
uint8_t tx_buffer[WIFI_MESSAGE_SIZE] = {0};

#if WFTCP_MODE == 0

static void WF_SetCommunicateMode(void) {
    sprintf(buffer, "%s", SetCommunicateMode);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetServerMode(void) {
    sprintf(buffer, "%s", SetServerMode);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetSend(uint16_t ByteSize) {
    static uint8_t LinkNum = 0;
    static uint8_t num = 0;

    switch (num) {
        case 0:
            sprintf(buffer, "%s%d,%d\r\n", SetCommunicateMode, LinkNum, ByteSize);
            HAL_UART_Transmit(&huart8, (uint8_t *) buffer, sizeof(buffer), 1000);
            while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
            memset(buffer, 0, sizeof buffer);
            num++;
            break;
        case 1:
            if (flag_wifiFinish) {
                flag_wifiFinish = false;
                num++;
            }
            if (flag_linkError) {
                num--;
                LinkNum++;
            }
            break;
        default:
            break;
    }

    sprintf(buffer, "%s%d,%d\r\n", SetCommunicateMode, LinkNum, ByteSize);
    HAL_UART_Transmit(&huart8, (uint8_t *) buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

//static void arrayCpy(uint8_t *tx_data, uint16_t size){
//    for (int i = 0; i < size; ++i) {
//        tx_buffer[i] = *(tx_data+i* sizeof(uint8_t));
//    }
//    tx_buffer[size] = '\0';
//}

static void WF_SendMessage(uint8_t linkNum, uint8_t *tx_data, uint16_t size) {
    WF_SetSend(size);
    HAL_UART_Transmit(&huart8, tx_data, sizeof(tx_data), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
}

void WiFi_Init(void) {
    static uint8_t num = 0;
    while (flag_finsh) {
        switch (num) {
            case 0:
                WF_SetCommunicateMode();
                num++;
                HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
                break;
            case 1:
                if (flag_wifiFinish) {
                    num++;
                    flag_wifiFinish = false;
                }
                HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
                break;
            case 2:
                WF_SetServerMode();
                HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
                num++;
            case 3:
                if (flag_wifiFinish) {
                    num++;
                    flag_wifiFinish = false;
                }
                HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
                break;
            default:
                flag_finsh = false;
            // printf("OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n");
                break;
        }
        osDelay(20);
    }
}

#endif

#if WFTCP_MODE == 1

static void WF_SetRestore(void) {
    sprintf(buffer, "%s", SetRestore);
    HAL_UART_Transmit(&huart4, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetWFMode(void) {
    sprintf(buffer, "%s", SetWFMode);
    HAL_UART_Transmit(&huart4, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetConnectAP(void) {
    sprintf(buffer, "%s\"%s\",\"%s\"\r\n", SetConnectAP, ssid, key);
    HAL_UART_Transmit(&huart4, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetConnectTCP(void) {
    sprintf(buffer, "%s\"%s\",\"%s\",%s\r\n", SetConnectTCP, WiFi_Mode, remote_host, remote_port);
    HAL_UART_Transmit(&huart4, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetCIPCommunicateMode(void) {
    sprintf(buffer, "%s", SetCIPCommunicateMode);
    HAL_UART_Transmit(&huart4, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetTransparentMode(void) {
    sprintf(buffer, "%s", SetTransparentMode);
    HAL_UART_Transmit(&huart4, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SendData(uint8_t *tx_data) {
    HAL_UART_Transmit(&huart4, tx_data, sizeof(*tx_data), 1000);
    while (HAL_UART_GetState(&huart4) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

void WiFi_Init(void) {
    static uint8_t num = 0;
    switch (num) {
        case 0:
            WF_SetRestore();
            num++;
            break;
        case 1:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 2:
            HAL_Delay(1000);
            WF_SetWFMode();
            num++;
            break;
        case 3:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 4:
            HAL_Delay(2000);
            WF_SetConnectAP();
            num++;
            break;
        case 5:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            if(flag_connectFinish) {
                num--;
                flag_connectFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 6:
            HAL_Delay(500);
            WF_SetConnectTCP();
            num++;
            break;
        case 7:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 8:
            WF_SetCIPCommunicateMode();
            num++;
            break;
        case 9:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 10:
            WF_SetTransparentMode();
            num++;
            break;
        case 11:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        default:
            break;
    }
    // printf("%d\r\n",num);
}

#endif

#endif

#if WFMODE == 0

const char *SetRestore = "AT+RESTORE\r\n";
const char *SetWFMode = "AT+CWMODE=3\r\n";
const char *SetConnectAP = "AT+CWJAP=";
const char *SetConnectTCP = "AT+CIPSTART=";
const char *SetCIPCommunicateMode = "AT+CIPMODE=1\r\n";
const char *SetTransparentMode = "AT+CIPSEND\r\n";

const char *SetCommunicateMode = "AT+CIPMUX=1\r\n";
const char *SetServerMode = "AT+CIPSERVER=1\r\n";
const char *Send = "AT+CIPSEND=";

const char *ssid = "ATK_6253FF";
const char *key = "";
const char *WiFi_Mode = "UDP";
const char *remote_host_R1 = "192.168.43.32";
const char *remote_host_handshake = "192.168.43.11";
const char *remote_port = "333";
const char *locate_port = "333";
const char *UDP_MODE = "0";
const char *link_num = "4";

uint8_t WiFi_rx[WIFI_MESSAGE_SIZE] = {0};
uint8_t buffer[WIFI_MESSAGE_SIZE] = {0};
uint8_t tx_buffer[WIFI_MESSAGE_SIZE] = {0};

#if WFTCP_MODE == 0

static void WF_SetCommunicateMode(void) {
    sprintf(buffer, "%s", SetCommunicateMode);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetServerMode(void) {
    sprintf(buffer, "%s%s,\"%s\",\"%s\",%s,%s,%s\r\n", SetConnectTCP,link_num,WiFi_Mode,remote_host_handshake,remote_port,locate_port,UDP_MODE);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetSend(uint16_t ByteSize) {

    sprintf(buffer, "%s%s,%d\r\n", SetCommunicateMode,link_num, ByteSize);
    HAL_UART_Transmit(&huart8, (uint8_t *) buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

//static void arrayCpy(uint8_t *tx_data, uint16_t size){
//    for (int i = 0; i < size; ++i) {
//        tx_buffer[i] = *(tx_data+i* sizeof(uint8_t));
//    }
//    tx_buffer[size] = '\0';
//}

static void WF_SendMessage(uint8_t *tx_data) {
    WF_SetSend(sizeof(tx_data));
    HAL_UART_Transmit(&huart8, tx_data, sizeof(tx_data), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
}
void WiFi_Init(void) {
    static uint8_t num = 0;
    while (flag_finsh) {
        switch (num) {
            case 0:
                WF_SetCommunicateMode();
                num++;
                // HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);
                break;
            case 1:
                if (flag_wifiFinish) {
                    num++;
                    flag_wifiFinish = false;
                }
                // HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);
                break;
            case 2:

                WF_SetServerMode();
                // HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);
                num++;
            case 3:
                if (flag_wifiFinish) {
                    num++;
                    flag_wifiFinish = false;
                }
                HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3);
                break;
            default:
                flag_finsh = false;
            // printf("OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n");
                break;
        }
        osDelay(50);
    }
}

#endif

#if WFTCP_MODE == 1

static void WF_SetConnectTCP(void) {
    sprintf(buffer, "%s\"%s\",\"%s\",%s,%s,%s\r\n", SetConnectTCP, WiFi_Mode, remote_host_R1, remote_port,locate_port,UDP_MODE);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetCIPCommunicateMode(void) {
    sprintf(buffer, "%s", SetCIPCommunicateMode);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}

static void WF_SetTransparentMode(void) {
    sprintf(buffer, "%s", SetTransparentMode);
    HAL_UART_Transmit(&huart8, buffer, sizeof(buffer), 1000);
    while (HAL_UART_GetState(&huart8) == HAL_UART_STATE_BUSY_TX);
    memset(buffer, 0, sizeof buffer);
}


void WiFi_Init(void) {
    static uint8_t num = 0;
    switch (num) {
        case 0:
            HAL_Delay(500);
            WF_SetConnectTCP();
            num++;
            break;
        case 1:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 2:
            WF_SetCIPCommunicateMode();
            num++;
            break;
        case 3:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        case 4:
            WF_SetTransparentMode();
            num++;
            break;
        case 5:
            if (flag_wifiFinish) {
                num++;
                flag_wifiFinish = false;
            }
            HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);
            break;
        default:
            break;
    }
    // printf("%d\r\n",num);
}

#endif

#endif

